<?php

namespace app\common\controller;
use app\admin\controller\User;
use app\common\model\AdminFile;
use app\common\model\RuinuoApp;
use app\common\model\RuinuoDatabase;
use app\common\model\RuinuoLists;
use app\common\model\RuinuoMode;
use app\common\service\Excel as ExcelService;
use think\facade\Request;

class Component
{

    public function BaiduMap()
    {
        return ViewTpl('common/component/baidumap');
    }

    public function Gongshi()
    {
        return ViewTpl('common/component/gongshi');
    }

    public function BindEvent()
    {
        return ViewTpl('common/component/bindevent');
    }

    public function NumRule()
    {
        return ViewTpl('common/component/numrule');
    }
    public function OpenSelect()
    {
        return ViewTpl('common/component/openselect');
    }
    public function ListTpl()
    {
        return ViewTpl('common/component/listtpl');
    }
    public function Excel()
    {
        if (Request::isAjax() || Request::isPost()) {
            $file = Request::file("file") ? : Request::file("edit");
            $ExcelService = new ExcelService();
            $params = $ExcelService->Import($file);
            if ($params['code'] != 200) {
                return BackCode($params['data'],$params['code'],$params['msg']);
            }
            $arr = $params['data'];
            return BackCode($arr);
        }
        return ViewTpl('common/component/excel');
    }

    public function JsonTable()
    {
        if (Request::isAjax() || Request::isPost()) {

            $data=[
                'detail'=>[],
                'source'=>[
                    'verify_list' => GetOptions('verify_list'),
                    'update_type'=>[
                        'rows' => [
                            ['label' => '文本框', 'value' => 'input'],
                            ['label' => '下拉框', 'value' => 'select'],
                            ['label' => '年月日', 'value' => 'date'],
                            ['label' => '年月日时分秒', 'value' => 'time'],
                            ['label' => 'switch', 'value' => 'switch'],
                        ],
                    ],
                    'options_list'=>GetOptions('options_list'),
                ]
            ];

            if (input("database"))
            {
                $Database=new RuinuoDatabase();
                $field=$Database->where("name='".input("database")."'")->field("field")->findOrEmpty()->toArray();
                $field=!empty($field['field']) ? json_decode($field['field'],true) : [];
                $database_field=[];
                foreach ($field as $k=>$v)
                {
                    $database_field[]=[
                        'label'=>$v['title'].'_'.$v['field'],
                        'value'=>$v['field'],
                    ];
                }
                $data['source']['database_field']['rows']=$database_field;
            }

            return BackCode($data);
        };
        return ViewTpl('common/component/jsontable');
    }

    public function SubTable()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoMode=new RuinuoMode();
            $children=$RuinuoMode->where("id='" . input('mode_id') . "'")->field("database")->findOrEmpty()->toArray();
            $database=$this->DataBase($children['database']);
            $data=[
                'detail'=>[],
                'source'=>[
                    'options_list'=>GetOptions('options_list'),
                    'database_field'=>[
                        'rows'=>$database['rows'][0]['children']
                    ],
                ]
            ];
            return BackCode($data);
        };
        return ViewTpl('common/component/subtable');
    }

    public function ChooseTable()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoMode=new RuinuoMode();
            $children=$RuinuoMode->where("id='" . input('mode_id') . "'")->field("database")->findOrEmpty()->toArray();
            $database=$this->DataBase($children['database']);
            $data=[
                'detail'=>[],
                'source'=>[
                    'options_list'=>GetOptions('options_list'),
                    'database_field'=>[
                        'rows'=>$database['rows'][0]['children']
                    ],
                ]
            ];
            return BackCode($data);
        };
        return ViewTpl('common/component/choosetable');
    }



    public function DataBase($num = '',$alias='')
    {
        $RuinuoDatabase = new RuinuoDatabase();
        $list = $RuinuoDatabase->where("name='" . $num . "'")->field("name,comment,field")->select()->toArray();
        $arr = [];
        foreach ($list as $k => $v) {
            $name='';
            if ($alias)
            {
                $name='(子表)';
            }
            $arr[$k]['label'] = $v['comment'] . $v['name'].$name;
            $arr[$k]['value'] = $v['name'];
            $arr[$k]['children'] = [];
            $field = json_decode($v['field'], true);
            foreach ($field as $kk => $vv) {
                if ($vv['field'] != "id" && $vv['field'] != "create_time" && $vv['field'] != "update_time" && $vv['field'] != "create_id" && $vv['field'] != "update_id") {
                    $value=$vv['field'];
                    if ($alias)
                    {
                        $value=$alias.'__'. $vv['field'];
                    }
                    $arr[$k]['children'][] = [
                        'label' => $vv['title'].'_' . $vv['field'],
                        'value' => $value,
                        'text' => $vv['title'],
                    ];
                }
            }
        }
        $data['rows'] = $arr;
        return $data;
    }













}